<?php
namespace app\sadmin\controller;
use think\Controller;
use think\Db;

class Recom extends Controller
{
   public function index(){
       	$recom=Db::name("recom")->select();
       	// dump($shop);exit;
       	$this->assign("recom",$recom);
       	return $this->fetch();
       }
      public function delete($id){
       	//先把对应的商品头像删除
          $recom=Db::name("recom")->find($id);
          $pic=$recom["recommentpic"];
          $file=ROOT_PATH."public".DS."static".DS."images".DS.$pic;
          if(file_exists($file))unlink($file);

       	if(Db::name("recom")->delete($id)) 
       	{
       	   $this->success('删除成功');
       	}else $this->fail('删除失败');
       }
       public function add(){
    return $this->fetch();
}

public function recomadd(){
       $file = request()->file('pic'); 
        //移动到框架应用根目录/public/static/images/ 目录下 
       var_dump($file);
        if($file){
          $info = $file->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'static'. DS . 'imagerecom'); 
          if ($info) { 
              $filename=$info->getFilename(); 
          } else { 
              $this->error($file->getError()); 
          } 
        }

    if(request()->isPost()){
     
      $data=[

        'recommentpic'=>$filename,
        'recommentname'=>input('param.recommentname'),
        'recommentintro'=>input('param.recommentintro'),
      ];
    }
 
   $res=Db::name("recom")->insert($data);
    if($res){
      $this->redirect('recom/index',5,'新增推荐成功');
    }else{
      $this->error("新增推荐失败");
    }
    return $this->fetch();
  }

public function edit(){
    $id=input('param.id');
    $recommentid=Db::name('recom')->find($id);
    $this->assign("recom",$recommentid);
    return $this->fetch();
}

public function update(){
   $file = request()->file('pic'); 
        //移动到框架应用根目录/public/static/images/ 目录下 
       var_dump($file);
        if($file){
          $info = $file->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'static'. DS . 'imagerecom'); 
          if ($info) { 
              $filename=$info->getFilename(); 
          } else { 
              $this->error($file->getError()); 
          } 
        }

    if(request()->isPost()){
     
      $data=[
        'recommentid'=>input('param.recommentid'),
        'recommentname'=>input('param.recommentname'),
        'recommentpic'=>$filename,
        'recommentintro'=>input('param.recommentintro'),
         ];
    }
 
   $res=Db::name("recom")->update($data);
    if($res){
      $this->redirect('recom/index',5,'修改推荐成功');
    }else{
      $this->error("修改推荐失败");
    }
    return $this->fetch();
  }
}